﻿$(function () { 
    $(document).mouseup(function (e) {
        var titleContainer = $('.title');
        var descContainer = $('.description');
        if (titleContainer.has(e.target).length === 0) {
            if (titleContainer.children('.edit').is(':visible')) {
                var titleTxt = titleContainer.children('.edit').children('input').val();
                titleContainer.children('.display').children('h2').children('span').text(titleTxt);
                titleContainer.children('.display').show();
                titleContainer.children('.edit').hide();
            }
        }
        if (descContainer.has(e.target).length === 0) {
            if (descContainer.children('.edit').is(':visible')) {
                var descTxt = descContainer.children('.edit').children('textarea').val();
                descContainer.children('.display').children('p').text(descTxt);
                descContainer.children('.display').show();
                descContainer.children('.edit').hide();
            }
        }
    });

    $('.page-title > div').click(function () {
        if ($(this).children('.display').is(':visible')) {
            if ($(this).hasClass('title')) {
                var titleTxt = $(this).children('.display').children('h2').children('span').text();
                $(this).children('.edit').children('input').val(titleTxt);
            }
            else {
                if ($(this).hasClass('description')) {
                    var descTxt = $(this).children('.display').children('p').text();
                    $(this).children('.edit').children('textarea').val(descTxt);
                }
            }
            $(this).children('.display').hide();
            $(this).children('.edit').show();
        }
    });

    $('.page-title').children('div').mouseover(function () {
        if ($(this).children('.display').is(':visible')) {
            $(this).addClass('hover');
        }
    });
    $('.page-title').children('div').mouseout(function () {
        $(this).removeClass('hover');
    });
});